package com.xindaoapp.henghua.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xindaoapp.henghua.R;
import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.entry.UnSubmitLabelInfo;
import com.xindaoapp.henghua.utils.CommFunc;
import com.xindaoapp.henghua.utils.MyBaseAdapter;
import com.xindaoapp.henghua.utils.UpdateDataChanged.OnUpdateDataChangedListener;

/**
 * 未提交标签
 * @author xyf
 *
 */
public class UnSubmitActivity extends BaseActivity {

	private ListView listView;
	private UnSubmitAdapter unSubmitAdapter;
	private TextView tvTitle;
	private List<UnSubmitLabelInfo> unSubmitLabelInfos = new ArrayList<UnSubmitLabelInfo>();

	private final OnUpdateDataChangedListener mUpdateDataChangedListener = new OnUpdateDataChangedListener() {

		@Override
		public void onUpdateDataChanged() {
			initWidget();

			CharSequence text = getString(R.string.unSubmitNums, unSubmitLabelInfos.size());
			tvTitle.setText(text);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_unsubmit);
		initWidget();
		XinDaoApplication.get().getUpdateDataChanged().add(mUpdateDataChangedListener);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		XinDaoApplication.get().getUpdateDataChanged().remove(mUpdateDataChangedListener);
	}

	private void initWidget() {

		tvTitle = getTvTitle();
		listView = (ListView) findViewById(R.id.listview);

		unSubmitLabelInfos = XinDaoApplication.get().getDBHelper().getUnSubmitLabels();

		unSubmitAdapter = new UnSubmitAdapter(this, unSubmitLabelInfos, R.layout.itme_unsubmit_item);

		listView.setAdapter(unSubmitAdapter);

	}

	@Override
	protected void onStart() {
		super.onStart();

		CharSequence text = getString(R.string.unSubmitNums, unSubmitLabelInfos.size());
		tvTitle.setText(text);

	}

	@Override
	String getTabTitle() {
		return "未提交标签";
	}

	@SuppressLint("ResourceAsColor")
	class UnSubmitAdapter extends MyBaseAdapter<UnSubmitLabelInfo> {

		public UnSubmitAdapter(Context context, List<UnSubmitLabelInfo> mList, int resId) {
			super(context, mList, resId);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent, UnSubmitLabelInfo t) {
			ViewHolder vh = (ViewHolder) convertView.getTag();
			if (vh == null) {
				vh = new ViewHolder();
				vh.ll = (LinearLayout) convertView.findViewById(R.id.layout_ll);
				vh.tv_deviceId = (TextView) convertView.findViewById(R.id.tv_deviceId);
				vh.tv_deviceName = (TextView) convertView.findViewById(R.id.tv_deviceName);
				vh.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
				vh.tv_scanPart = (TextView) convertView.findViewById(R.id.tv_scanPosition);
				vh.tv_scanLocation = (TextView) convertView.findViewById(R.id.tv_scanLocation);
				vh.tv_scanTime = (TextView) convertView.findViewById(R.id.tv_scanTime);
				convertView.setTag(vh);
			}

			if (position % 2 == 0) {
				vh.ll.setBackgroundColor(Color.parseColor("#ffffff"));
			} else {
				vh.ll.setBackgroundColor(Color.parseColor("#eff8fe"));
			}

			// TODO null处理？？？
			if (null != t.scanData) {
				vh.tv_deviceId.setText(t.code);
//				System.out.println("getView +++++++ " + t.scanData.scan_devicename);
				vh.tv_deviceName.setText(t.name);
				vh.tv_code.setText(t.scanData.tag_code);
				vh.tv_scanPart.setText(t.propertyLabelInfo.part);
				//先取物理地址，取不到去坐标值，再取不到显示位置未取到
				vh.tv_scanLocation.setText("".equals(t.scanData.scan_location)?"位置未取到":t.scanData.scan_location);

				String time = CommFunc.getTime(t.scanData.scan_time);
				vh.tv_scanTime.setText(time);
			}

			return convertView;
		}

	}

	@Override
	protected int onSubmitButtonText() {
		return R.string.one_key_submit;
	}

	@Override
	protected boolean onShowDownloadButton() {
		return false;
	}

	@Override
	protected boolean onShowSubmitButton() {
		if (isHaveUnSubmitLabel()) {
			return true;
		} else {
			return false;
		}
	}

	private static class ViewHolder {
		private LinearLayout ll;
		private TextView tv_deviceId;
		private TextView tv_deviceName;
		private TextView tv_code;
		private TextView tv_scanPart;
		private TextView tv_scanLocation;
		private TextView tv_scanTime;
	}

}
